package com.example.demo.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;

/**
 * Created by liulanhua on 2018/4/26.
 */
@RestController
@RequestMapping("/valid")
public class CaptchaController {

    @ApiOperation(value = "获取认证码", notes = "获取认证码接口", httpMethod = "GET")
    @GetMapping(value = "/captcha")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sessionId = request.getSession().getId();
        ICaptcha captcha = CaptchaUtil.createGifCaptcha(60, 26, 4);
        response.setContentType("image/gif");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        request.getSession().setAttribute("captcha", captcha);
        captcha.write(response.getOutputStream());
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }


}
